﻿using Dt261.Model;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dt261.db
{
    class PureToneOptionsDb
    {
        private readonly SQLiteConnection connection;

        public PureToneOptionsDb(string dbPath)
        {
            connection = new SQLiteConnection(dbPath);
            connection.CreateTable<PureToneOptions>();
        }

        public int AddPureToneOptions(PureToneOptions m)
        {

            return connection.Insert(new PureToneOptions()
            {
                Uid = m.Uid,
                DefaultValue = m.DefaultValue,
                IsAutoJump = m.IsAutoJump,
                IsChecked125 = m.IsChecked125,
                IsChecked250 = m.IsChecked250,
                IsChecked500 = m.IsChecked500,
                IsChecked750 = m.IsChecked750,
                IsChecked1000 = m.IsChecked1000,
                IsChecked1500 = m.IsChecked1500,
                IsChecked2000 = m.IsChecked2000,
                IsChecked3000 = m.IsChecked3000,
                IsChecked4000 = m.IsChecked4000,
                IsChecked6000 = m.IsChecked6000,
                IsChecked8000 = m.IsChecked8000,
                IsMaintain = m.IsMaintain,

                StepValue = m.StepValue,
                UserId = m.UserId,
 

    });
        }

        public int UpdatePureToneOptions(PureToneOptions m)
        {


            IList<PureToneOptions> list = connection.Query<PureToneOptions>("select * from PureToneOptions where id = ?", m.Id);

            if (list.Count() > 0)
            {
                return connection.Update(m);
            }
            else
            {
                return this.AddPureToneOptions(m);
            }
        }

        public IList<PureToneOptions> GetMembers()
            => connection.Table<PureToneOptions>().ToList();

        public IList<PureToneOptions> GetMemberByUid(string uid)
        {
            IList<PureToneOptions> list = connection.Query<PureToneOptions>("select * from PureToneOptions where uid = ?", uid);

            return list;
        }

        public IList<PureToneOptions> GetPureToneOptionsByUserId(string userid)
        {
            IList<PureToneOptions> list = connection.Query<PureToneOptions>("select * from PureToneOptions where UserId = ?", userid);

            return list;
        }

        public PureToneOptions GetOnePureToneOptionsByUid(string uid)
        {
            IList<PureToneOptions> list = connection.Query<PureToneOptions>("select * from PureToneOptions where uid = ?", uid);

            return list.Count() > 0 ? list[0] : null;
        }

        public PureToneOptions GetOnePureToneOptionsById(int id)
        {
            IList<PureToneOptions> list = connection.Query<PureToneOptions>("select * from PureToneOptions where id = ?", id);

            return list.Count() > 0 ? list[0] : null;
        }

        public PureToneOptions GetOnePureToneOptionsByUserId(string userid)
        {
            IList<PureToneOptions> list = connection.Query<PureToneOptions>("select * from PureToneOptions where UserId = ?", userid);

            if (list.Count() > 0)
                return list[0];
            else
            {
                return new PureToneOptions()
                {
                    Uid = System.Guid.NewGuid().ToString("N"),
                    DefaultValue = 30,
                    IsAutoJump = false,
                    IsChecked125 = true,
                    IsChecked250 = true,
                    IsChecked500 = true,
                    IsChecked750 = true,
                    IsChecked1000 = true,
                    IsChecked1500 = true,
                    IsChecked2000 = true,
                    IsChecked3000 = true,
                    IsChecked4000 = true,
                    IsChecked6000 = true,
                    IsChecked8000 = true,
                    IsMaintain = true,

                    StepValue = 5,
                    UserId = GlobalData.LoginUser.Uid,


                };
            }
        }
        public int DeletePureToneOptions(PureToneOptions m)
        {
            return connection.Delete(m);
        }
    }
}
